package cn.iocoder.yudao.module.trade.service.depositorderlackbottles;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.api.user.CrmMemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.CrmMemberUserRespDTO;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.trade.controller.admin.depositorderlackbottlesrelation.vo.DepositOrderLackBottlesRelationRespVO;
import cn.iocoder.yudao.module.trade.dal.dataobject.depositorderlackbottlesrelation.DepositOrderLackBottlesRelationDO;
import cn.iocoder.yudao.module.trade.dal.mysql.depositorderlackbottlesrelation.DepositOrderLackBottlesRelationMapper;
import cn.iocoder.yudao.module.trade.service.depositorderlackbottlesrelation.DepositOrderLackBottlesRelationService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.trade.controller.admin.depositorderlackbottles.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.depositorderlackbottles.DepositOrderLackBottlesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.trade.dal.mysql.depositorderlackbottles.DepositOrderLackBottlesMapper;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;

/**
 * 欠瓶记录 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DepositOrderLackBottlesServiceImpl implements DepositOrderLackBottlesService {

    @Resource
    private DepositOrderLackBottlesMapper depositOrderLackBottlesMapper;

    @Resource
    private DeptApi deptApi;

    @Resource
    private DepositOrderLackBottlesRelationMapper depositOrderLackBottlesRelationMapper;

    @Resource
    private DepositOrderLackBottlesRelationService depositOrderLackBottlesRelationService;

    @Resource
    private CrmMemberUserApi crmMemberUserApi;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    @Transactional
    public Long createDepositOrderLackBottles(DepositOrderLackBottlesSaveReqVO createReqVO) {
        // 插入
        DepositOrderLackBottlesDO depositOrderLackBottles = BeanUtils.toBean(createReqVO, DepositOrderLackBottlesDO.class);
        depositOrderLackBottlesMapper.insert(depositOrderLackBottles);

        depositOrderLackBottlesRelationService.insertBathRelation(createReqVO.getRelationRespVOList());
        // 返回
        return depositOrderLackBottles.getId();
    }

    @Override
    public void updateDepositOrderLackBottles(DepositOrderLackBottlesSaveReqVO updateReqVO) {
        // 校验存在
        validateDepositOrderLackBottlesExists(updateReqVO.getId());
        // 更新
        DepositOrderLackBottlesDO updateObj = BeanUtils.toBean(updateReqVO, DepositOrderLackBottlesDO.class);
        depositOrderLackBottlesMapper.updateById(updateObj);
    }

    @Override
    public void deleteDepositOrderLackBottles(Long id) {
        // 校验存在
        validateDepositOrderLackBottlesExists(id);
        // 删除
        depositOrderLackBottlesMapper.deleteById(id);
    }

    private void validateDepositOrderLackBottlesExists(Long id) {
        if (depositOrderLackBottlesMapper.selectById(id) == null) {
            throw exception(DEPOSIT_ORDER_LACK_BOTTLES_NOT_EXISTS);
        }
    }

    @Override
    public DepositOrderLackBottlesRespVO getDepositOrderLackBottles(Long id) {
        DepositOrderLackBottlesRespVO bean = BeanUtils.toBean(depositOrderLackBottlesMapper.selectById(id), DepositOrderLackBottlesRespVO.class);
        CrmMemberUserRespDTO member = crmMemberUserApi.getCrmMemberById(bean.getCrmGasUserId());
        bean.setAccountType(member.getAccountType());
        bean.setCrmGasCode(member.getCode());
        List<DepositOrderLackBottlesRelationRespVO> relationRespVOList = depositOrderLackBottlesRelationService.getDepositOrderRelationByLackId(id);


        bean.setRelationRespVOList(relationRespVOList);
        return bean;
    }

    @Override
    public PageResult<DepositOrderLackBottlesRespVO> getDepositOrderLackBottlesPage(DepositOrderLackBottlesPageReqVO pageReqVO) {
        PageResult<DepositOrderLackBottlesDO> page = depositOrderLackBottlesMapper.selectPage(pageReqVO);
        if (CollectionUtils.isNotEmpty(page.getList())) {
            PageResult<DepositOrderLackBottlesRespVO> bean = BeanUtils.toBean(page, DepositOrderLackBottlesRespVO.class);
            bean.getList().forEach(x -> {
                x.setDeptName(x.getDeptId() == null ? null : deptApi.getDept(x.getDeptId()).getName());
            });

            return bean;
        }

        return new PageResult<>();
    }

}